package com.yy.main.app.service.impl;

import com.yy.kit.collections.YYMapKits;
import com.yy.kit.string.YYStrKits;
import com.yy.main.app.mapper.TUserRoleMapper;
import com.yy.main.app.service.IUserRoleService;
import com.yy.pojo.base.TUserRole;
import com.yy.pojo.base.TUserRoleExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 用户与角色的关联表 服务实现类
 * </p>
 *
 * @author 杨阳
 * @since 2017-08-22
 */
@Service("iUserRoleService")
public class UserRoleServiceImpl implements IUserRoleService {

    @Autowired
    private TUserRoleMapper tUserRoleMapper;

    @Override
    public Map<String, Object> assign(String role, String userid) {
        Map<String, Object> res = YYMapKits.createHashMap();
        // 1.删除用户原来拥有的角色
        TUserRoleExample example = new TUserRoleExample();
        example.createCriteria().andUser_idEqualTo(userid);
        Integer in = tUserRoleMapper.deleteByExample(example);

        // 2. 再插入t_user_role表
        TUserRole ur = new TUserRole();
        ur.setId(YYStrKits.getTableId());
        ur.setUser_id(userid);
        ur.setRole_id(role);
        in = tUserRoleMapper.insert(ur);
        if (in > 0) {
            res.put("success", Boolean.TRUE);
            res.put("msg", "授权成功!");
        } else {
            res.put("success", Boolean.FALSE);
            res.put("msg", "授权失败,请重试!");
        }
        return res;
    }

}
